
/*
led rgb
共阴Led，长为阴，接近长的为R，依次为R，阴，G，B
每个引脚加一个220Ω电阻
*/

const int ledR = 3;
const int ledG = 5;
const int ledB = 6;

void setup() {
  for(int i = 3; i < 7; ++i)
  {
    pinMode(i, OUTPUT);
  }
}

void loop() {

  light(ledR,256, 10);
  delay(200);
  dark(ledR,256, 10);
  light(ledG,256, 10);
  delay(100);
  dark(ledG,256, 10);
  light(ledB,256, 10);
  delay(100);
  dark(ledB,256, 10);
  delay(500);  

  dance();
}

void dance()
{
  randomSeed(analogRead(A0));
  for(int i = 0; i < 100; ++i)
  {
    int n = random(0,3);
    int dl = random(1,100);
    int led = n == 0 ? 3 : n == 1 ? 5 : 6;
    digitalWrite(led, HIGH);
    delay(dl);
    digitalWrite(led, LOW);
    delay(10);
  }
}

void light(int led, int bright, int sp)
{
  for(int i = 0; i < bright; ++i)
  {
    analogWrite(led, i);
    delay(sp);
  }
}

void dark(int led, int bright,int sp)
{
  for(int i = bright; i > 0; --i)
  {
    analogWrite(led, i);
    delay(sp);
  }
}
